// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode')
const { fileUpload, changeText } = require('./util/upload')
const CreateFile = require('./util/createFile')
/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {
  // Use the console to output diagnostic information (console.log) and errors (console.error)
  // This line of code will only be executed once when your extension is activated
  console.log('Congratulations, your extension "xxmj-upload" is now active!')

  // The command has been defined in the package.json file
  // Now provide the implementation of the command with  registerCommand
  // The commandId parameter must match the command field in package.json

  // 图片/文件 上传
  let fileSelect = vscode.commands.registerCommand(
    'xxmj-upload.choosedFile',
    async (textEditor, edit, args) => {
      // The code you place here will be executed every time your command is executed
      // Display a message box to the user
      // vscode.window.showInformationMessage('Hello World from xxmj-upload!')
      const fileList = await vscode.window.showOpenDialog({
        canSelectFolders: false, // 是否可以选择文件夹,只能选择文件
        canSelectMany: false, // 是否多选
        filters: {
          images: [
            'jpg',
            'png',
            'jpeg',
            'webp',
            'gif',
            'ico',
            'svg',
            'mp3',
            'mp4',
            'mov',
            'js',
            'ts',
            'vue',
            'jsx',
            'tsx',
            'json'
          ]
        }
      })
      fileList && fileUpload(fileList)
    }
  )

  // imgcook 图片转换
  let fileChange = vscode.commands.registerCommand(
    'xxmj-upload.changeFile',
    (textEditor, edit, args) => {
      const filePath = textEditor.path
      changeText(filePath)
    }
  )

  // taro 小程序文件夹创建
  let createTaroDir = vscode.commands.registerCommand(
    'xxmj-upload.createTaroDir',
    (param, edit, args) => {

      const options = {
        prompt: '请输入要创建的Page名称',
        placeHolder: 'Page文件夹名称',
      };
      vscode.window.showInputBox(options).then((value) => {
        if (!value) {
          return;
        }
        const pageName = value;
        if (param) {
          const folderPath = param.fsPath;
          CreateFile(folderPath, pageName);
        } else {
          console.log(vscode.window.activeTextEditor);
        }
      });
    }
  )

  context.subscriptions.push(fileSelect)
  context.subscriptions.push(fileChange)
  context.subscriptions.push(createTaroDir)
}
exports.activate = activate

// this method is called when your extension is deactivated
function deactivate() {}

module.exports = {
  activate,
  deactivate
}
